package cs2340.droidceptiontodo;

import java.io.FileOutputStream;
import java.io.PrintStream;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent; 
/**
 * This activity takes in inputs from the register screen.
 * It allows a user to create a new account so they can
 * use the application and save their work.
 * 
 */
public class RegisterActivity extends Activity implements OnClickListener 
{
    //Edittext 
    EditText login;
    EditText password;
    EditText personName;
    EditText email;
    //buttons
	Button createAccount;
	Button cancel;
	Database entry;
	 
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        Log.d("DEBUG","Starting Register Activity");
        //access editText3 of person name to get input
        personName = (EditText)findViewById(R.id.editText3);
        //access editText4 of email to get input
        email = (EditText)findViewById(R.id.editText4);
        //access editText5 of login id to get input
        login = (EditText)findViewById(R.id.editText5);
        //access editText6 of password to get input
        password = (EditText)findViewById(R.id.editText6);
        //access create account button to know when clicked
        createAccount = (Button)findViewById(R.id.button3);
        //access cancel button to know when clicked
        cancel = (Button)findViewById(R.id.button4);
        createAccount.setOnClickListener(this);
        cancel.setOnClickListener(this);
	    //entry = new Database(RegisterActivity.this);
        entry = Database.getInstance(RegisterActivity.this);
    }
	@Override
	/**
	 * Uses the information entered by the user to create and save
	 * a new user account.
	 */
	public void onClick(View v) {
		// Create account button has been clicked
	    if(v.getId() == (createAccount.getId())) {
			FileOutputStream fos =null; //to create a new account vs. reading existing one
			// Receive person name input text from edittext3
			String personNameStr = personName.getText().toString().trim();
			// Receive email input text from edittext4
			String emailStr = email.getText().toString().trim();
			// Receive login  input text from edittext5
			String loginStr = login.getText().toString().trim();
			// Receive password input text from edittext6
			String passwordStr = password.getText().toString().trim();
			Log.d("DEBUG","personNameStr"+personNameStr);
			Log.d("DEBUG","emailStr"+emailStr);
			Log.d("DEBUG","loginStr"+loginStr);
			Log.d("DEBUG","passwordStr"+passwordStr);
			
			TextView tv = (TextView)findViewById(R.id.registerinfo);
			//SQLite implementation
			//creates a database object (which in reality, just pulls up the old one)
			try{
				//opes the database
				entry.open();
				//adds a user to the usertable of the database
				entry.createUser(loginStr, passwordStr, emailStr, personNameStr);
				//closes the database
				entry.close();
				//if there are no errors, the account was created
				tv.setText("Congrats! new account creation successful!");
				finish();
			}catch (Exception e){
				tv.setText("Error creating account.");
			}

			
			/**
			try{
				Files.createUser(getApplicationContext(), loginStr, passwordStr, emailStr, personNameStr);
				//If there is any error, it will throw exception
				//If no error, indicates that account was created successfully
				TextView tv = (TextView)findViewById(R.id.registerinfo); //registerInfo is in register.xml
				tv.setText("Congrats! new account creation successful!");
			}
			catch(Exception e){
				TextView tv = (TextView)findViewById(R.id.registerinfo); //registerInfo is in register.xml
				tv.setText("Error creating account.");
			}
			*/
			//File name will be same as login
			
		}
	    //This is the case of Cancel button being clicked
		else {
			if (v.getId() == (cancel.getId())){
				finish();
			}
		}
	}
	public Database getDatabase(){
		return entry;
	}
}